package ai.peanut.selfbase.common.security.shiro.testshiro;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.junit.Test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ShiroAuthtication {

    private void login(String configFile) {
        //1、获取SecurityManager工厂，此处使用Ini配置文件初始化SecurityManager
        Factory<SecurityManager> factory =
                new IniSecurityManagerFactory(configFile);

        //2、得到SecurityManager实例 并绑定给SecurityUtils
        SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);

        //3、得到Subject及创建用户名/密码身份验证Token（即用户身份/凭证）
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");
        subject.login(token);
    }

    @Test
    public void testAllSuccessfulStrategyWithSuccess()  {

//
//
//        login("classpath:shiro.ini");
//
//        Subject subject = SecurityUtils.getSubject();
//
//        //得到一个身份集合，其包含了Realm验证成功的身份信息
//        PrincipalCollection principalCollection = subject.getPrincipals();
//
//        System.out.println( principalCollection.getRealmNames().size());
//        System.out.println(subject.isPermitted("user1:*") );
//        System.out.println(subject.isPermitted("user:*") );
//

        System.out.println(   new SecureRandomNumberGenerator().nextBytes().toHex());

        String algorithmName = "md5";
        String username = "admin";
        String password = "admin";
        String salt1 = username;
        String id = "1" ;
        String  salt2 = "26b868e4980a172ae6884ed1eddc0744" ;//new SecureRandomNumberGenerator().nextBytes().toHex();
        System.out.println(salt2);
        int hashIterations = 2;

        SimpleHash hash = new SimpleHash(algorithmName, password, salt1 + id+ salt2, hashIterations);
        String encodedPassword = hash.toHex();
        // e31622181daa556a7cff713b08f8b64c
        System.out.println(encodedPassword);


    }

    private void a(String b , String... a ){
        System.out.println( b ) ;

        System.out.println( a[1] ) ;
    }
    private class A {
        String a ;
        String b ;

        public String getA() {
            return a;
        }

        public void setA(String a) {
            this.a = a;
        }

        public String getB() {
            return b;
        }

        public void setB(String b) {
            this.b = b;
        }

        @Override
        public String toString() {
            return "A{" +
                    "a='" + a + '\'' +
                    ", b='" + b + '\'' +
                    '}';
        }
    }

}
